<?php
/**
 * Dispatch class definition
 *   
 * @author Sean Crystal <seancrystal@gmail.com>
 * @package MutantVole
 */
 
 /**
  * Handles URLs and associated Page classes. The application should, as much as possible, be
  * ignorant of the mechanics of URL navigation. Translating a URL request to an application
  * Request is kept isolated here.
  * @package MutantVole
  * @subpackage Base
  */    
class Dispatch {
	
	/**
     * Take in the URL request and application settings. Then setup Redirect
	 * and load the requested Page class.
	 * @param array $request
	 * @param array $settings
	 */    	
	public static function Init($request, $settings) {
		self::$mod      = $request['mod'] ? $request['mod'] : $settings['default']['mod'];
		self::$page     = $request['page'] ? $request['page'] : $settings['default']['page'];
		self::$action   = $request['action'];
		self::$request  = $request;		
		self::$pageFile = APP_DIR ."/". self::$mod ."/". self::$page .".php";
		
		if (!(file_exists(self::$pageFile))) {
			throw new PageNotFoundException(self::$mod, self::$page);
		}
	}	
	
	/**
     * Load a URL based on $mod and $page or URL and request params
	 * @param mixed $args
	 */  	
	public static function Redirect() {
		$params = func_get_args();
		$param = $params[0];
		
		if (is_array($param)) {
			foreach ($param as $key => $value) {
				switch ($key) {
					case 'url':
					case 'params':
						$params = array_key_exists('params', $param) ? '?'. http_build_query($param['params']) : null;
						
						header("Location: {$param['url']}{$params}");
						exit;
					case 'mod':
					case 'page':
						$mod = array_key_exists('mod', $param) ? $param['mod'] : self::$mod;
						$page = array_key_exists('page', $param) ? $param['page'] : null;
						
						header("Location: ". WEB_ROOT ."/{$mod}/{$page}");
						exit;
				}
			}
		} else {
			// try plain url
			header("Location: $param");
			exit;
		}
	}
	
	/**
	 * Get a proper URL for a $mod and $page
	 * @param array $params
	 * @return string
	 */
	public static function GetURL($params) {
		$mod  = $params['mod'] ? $params['mod'] : self::$mod;
		$page = $params['page'] ? $params['page'] : null;
		
		return WEB_ROOT ."/{$mod}/{$page}";
	}
	
	/**
     * Get an instance of the requested Page class
	 * @return Page
	 */  	
	public static function GetPage() {
		$class = self::$pageClass;
		
		if (!(class_exists($class))) {
			throw new ClassNotFoundException($class);
		}	
		
		return new $class;
	}
	
	/**
     * Get an instance of an Application Request
     * @return Request
	 */  
	public static function GetRequest() {
		return new Request(self::$mod, self::$page, self::$action, self::$request);
	}
	
	/**
     * Requested mod
	 * @var string
	 */  	
	public static $mod;
	
	/**
     * Requested page
	 * @var string
	 */  	
	public static $page;
	
	/**
     * Requested action
	 * @var string
	 */  	
	public static $action;
	
	/**
     * Entire request array
	 * @var array
	 */  	
	public static $request;
	
	/**
     * Full path to requested page
	 * @var string
	 */  	
	public static $pageFile;
	
	/**
     * Name of the class expected in page file
	 * @var string
	 */  	
	public static $pageClass;
}

?>
